﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MuzičkiFeršterker
{
    public partial class PlayList
    {
        /// <summary>
        /// Exports current playlist to M3U playlist format
        /// </summary>
        /// <param name="filepath">Location with desired file.</param>
        public void ExportToM3U(string filepath)
        {
            StreamWriter write = new StreamWriter(filepath);
            write.WriteLine("#EXTM3U");
            foreach (Song a in this.filesList)
            {
                write.WriteLine("#EXTINF:" + a.LengthInt / 1000 + ", " + a.Artist + " - " + a.Title + "\n" + a.Path);
            }
            write.Close();
        }

        /// <summary>
        /// Imports playlist file and appends it to current playlist
        /// </summary>
        /// <param name="filepath">File with playlist</param>
        public void ImportFromM3U(string filepath)
        {
            StreamReader read = new StreamReader(filepath);
            string line;
            while (!read.EndOfStream)
            {
                line = read.ReadLine().Trim(new char[] { ' ', '\n' });
                if (line.Length > 0 && line[0] != '#')
                {
                    if (File.Exists(line))
                        this.Add(line);
                }
            }
            read.Close();
        }
    }
}
